/// If 就是一个被条件包裹的Command， cmd是否被执行取决If携带的bool
pub struct If<T:crossterm::Command>(pub bool, pub T) ;

impl<T : crossterm::Command> crossterm::Command for If<T> {
    fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
        if self.0 {
            self.1.write_ansi(f)
        }else {
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests{
    use super::*;
    use crossterm::Command;
    #[test]
    fn test_if() {
        struct CommandA ;
        impl Command for CommandA {
            fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
                write!(f, "A cmd has been executed")
            }
        }
        let cmd_a = If(true , CommandA) ;
        let mut out = String::new() ;
        cmd_a.write_ansi(&mut out).unwrap();
        println!("{:}",out) ;

        // another case
        let cmd_b = If(false , CommandA );
        let mut out = String::new() ;
        cmd_b.write_ansi(&mut out).unwrap();
        println!("{:}" , out ) ;
    }
}